<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs @tab-click="handleClick">
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="add"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table
              v-loading="loading"
              border
              stripe
              :data="tableData"
              style="margin:20px 0"
            >
              <el-table-column :index="indexMothod" label="序号" type="index" width="100" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="primary">分配权限</el-button>
                  <el-button size="small" type="primary" @click="updateRole(row.id)">修改</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页
                current-page  当前的页码数
                page-sizes 下拉选择框 范围
                page-size 每页条数
                layout 布局 total, sizes, prev, pager, next, jumper
                total 总条数
                @size-change ="处理函数" 每页条数发生修改的时候会触发
                @current-change ="处理函数"  页码发生变化的时候会触发
             -->
            <el-pagination
              :current-page="queryInfo.page"
              :page-sizes="[2, 3, 5, 10]"
              :page-size="queryInfo.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-form :model="componyMsg" label-width="100px" style="width:400px">
              <el-form-item label="企业名称">
                <el-input v-model="componyMsg.name" disabled>1</el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="componyMsg.companyAddress" disabled>1</el-input>
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input v-model="componyMsg.companyPhone" disabled>1</el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="componyMsg.mailbox" disabled>1</el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="componyMsg.remarks" disabled>1</el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <!-- 添加角色,编辑角色对话框 -->
        <el-dialog
          :title="title"
          :visible.sync="dialogVisible"
          @close="closeDialog"
        >
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="form.description" />
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="closeDialog">取 消</el-button>
            <el-button type="primary" @click="handleSubmit">确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqAddRole, reqCompanyById, reqDelRoles, reqGetRolesList, reqRoleId, reqUpdateRole } from '@/api/setting'
export default {
  data() {
    return {
      activeName: 'role',
      tableData: [], // 角色数据
      total: 0, // 条数
      queryInfo: {
        page: 1,
        pagesize: 2
      },
      loading: false,
      dialogVisible: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: ['change', 'blur'] }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: ['change', 'blur'] }
        ]
      },
      title: '新增角色',
      componyMsg: {
        name: '',
        companyAddress: '',
        companyPhone: '',
        mailbox: '',
        remarks: ''
      } // 公司信息
    }
  },
  created() {
    //   获取所有角色列表
    this.getRolesList()
  },
  methods: {
    // 展示公司信息
    async handleClick(tab, event) {
      console.log(tab, event)
      if (tab.label === '公司信息') {
        const { data } = await reqCompanyById(this.$store.state.user.userInfo.companyId)
        this.componyMsg = data
      }
    },
    // 角色列表
    async getRolesList() {
      this.loading = true
      const { data: { rows, total }} = await reqGetRolesList(this.queryInfo)
      this.tableData = rows
      this.total = total
      this.loading = false
    },
    // 处理函数" 每页条数发生修改的时候会触发
    handleSizeChange(newPageSize) {
      // 将最新的pagesize更新后,重新发送请求
      this.queryInfo.pagesize = newPageSize
      this.getRolesList()
    },
    // "处理函数"  页码发生变化的时候会触发
    handleCurrentChange(newPage) {
      // 将最新的page更新后,重新发送请求
      this.queryInfo.page = newPage
      this.getRolesList()
    },
    indexMothod(index) {
      return index + 1 + this.queryInfo.pagesize * (this.queryInfo.page - 1)
    },
    // 删除角色
    async delRole(id) {
      const str = await this.$confirm('是否删除该角色', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (str !== 'confirm') return this.$message.info('已取消操作')
      await reqDelRoles(id)
      if (this.tableData.length === 1 && this.queryInfo.page > 1) {
        this.queryInfo.page--
      }
      this.getRolesList()
    },
    // 关闭对话框
    closeDialog() {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    // 点击确定按钮新增/编辑
    handleSubmit() {
      this.$refs.form.validate(async boo => {
        if (!boo) return
        if (this.form.id) {
          // 编辑
          await reqUpdateRole(this.form)
        } else {
          // 新增
          await reqAddRole(this.form)
        }
        this.closeDialog()
        this.getRolesList()
      })
    },
    // 编辑回显
    async updateRole(id) {
      this.title = '编辑角色'
      const { data } = await reqRoleId(id)
      this.form = data
      this.dialogVisible = true
    },
    // 新增角色点击按钮
    add() {
      this.dialogVisible = true
      this.title = '新增角色'
    }
  }
}
</script>

<style scoped>

</style>
